﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using uAccess;
using WebApi.Models;




namespace WebApi.Services
{
    public class MyInventory
    {
        //新增SKU
        public static Boolean CreateSKU(int pro_id, string sku_label1, string sku_label2, string sku_pic, string sku_code, int sku_stock, decimal? sku_cost, decimal? sku_refprice, decimal? sku_crtprice, int sku_wrnstock)
        {

            var op = new uAccess<Inventory>("dbConnect");
            var timeStamp = System.DateTime.Now;
            var rv = new Inventory()
            {
                pro_Id=pro_id,
                //SKU_Id = sku_id,
                SKU_Label = sku_label1,
                SKU_2ndLabel = sku_label2,
                SKU_Picture = sku_pic,
                SKU_Code = sku_code,
                SKU_Stock = sku_stock,
                SKU_Cost = sku_cost,
                SKU_RefPrice = sku_refprice,
                SKU_CrtPrice = sku_crtprice,
                SKU_WrnStock = sku_wrnstock,
                SKU_TimeStamp=timeStamp,

            };
            op.Add(rv);

            ////更新pro_sku
            //var op2 = new uAccess<Products>("dbConnect");
            //var pr = op2.GetFirst(c => c.pro_Id == pro_id);
            //var pro_sku = pr.pro_SKU + 1;
            //pr.pro_SKU = pro_sku;
            //op2.Update(pr);

           
            return true;

        }

        //显示所有SKU
        public static JArray SKUList(int pro_id)
        {
            JArray sku = new JArray();
            var op = new uAccess<Inventory>("dbConnect").GetAll(c => c.pro_Id==pro_id);
            var sku_str = JsonConvert.SerializeObject(op);
            sku = JArray.Parse(sku_str);
            return sku;
        }

        //显示某一个SKU信息
        public static JObject GetSKU(long sku_id)
        {

            uAccess<Inventory> op = new uAccess<Inventory>("dbConnect");
            var pr = op.GetFirst(c => c.SKU_Id == sku_id);
            JObject skuObj = new JObject
                {        
                    { "sku_id", pr.SKU_Id },
                    { "sku_label1", pr.SKU_Label },
                    { "sku_label2", pr.SKU_2ndLabel },
                    { "sku_pic", pr.SKU_Picture },
                    { "sku_code", pr.SKU_Code },
                    { "sku_stock", pr.SKU_Stock },
                    { "sku_cost", pr.SKU_Cost },
                    { "sku_refprice", pr.SKU_RefPrice },
                    { "sku_crtprice", pr.SKU_CrtPrice },
                    { "sku_wrnstock", pr.SKU_WrnStock },
                    
                };

            return skuObj;
        }

        //修改SKU信息
        public static Boolean ChangeSKU(int sku_id, string sku_label1, string sku_label2, string sku_pic, string sku_code, int sku_stock, decimal? sku_cost, decimal? sku_refprice, decimal? sku_crtprice, int sku_wrnstock)
        {
            var op = new uAccess<Inventory>("dbConnect");
            var pr = op.GetFirst(c => c.SKU_Id == sku_id);

            pr.SKU_Id = sku_id;
            pr.SKU_Label = sku_label1;
            pr.SKU_2ndLabel = sku_label2;
            pr.SKU_Picture = sku_pic;
            pr.SKU_Code = sku_code;
            pr.SKU_Stock = sku_stock;
            pr.SKU_Cost = sku_cost;
            pr.SKU_RefPrice = sku_refprice;
            pr.SKU_CrtPrice = sku_crtprice;
            pr.SKU_WrnStock = sku_wrnstock;

            op.Update(pr);
            return true;

        }

        //删除SKU
        public static Boolean DeleteSKU(int sku_id,int pro_id)
        {
            var op = new uAccess<Inventory>("dbConnect");
            var pr = op.GetFirst(c => c.SKU_Id == sku_id);
           // var id = pr.ID;
            var del_skuid = pr.SKU_Id;
            op.Delete(del_skuid);

            ////对应商品的pro_sku需要-1
            //var op2 = new uAccess<Products>("dbConnect");
            //var pr2 = op2.GetFirst(c => c.pro_Id == pro_id);
            //var pro_sku = pr2.pro_SKU - 1;
            //pr2.pro_SKU = pro_sku;
            //op2.Update(pr2);
    
            return true;


        }


        ////生成SKU_Id
        //public static JObject GetSKU_Id(long pro_id)
        //{
        //    var op = new uAccess<Products>("dbConnect");
        //    var pr = op.GetFirst(c => c.pro_Id == pro_id);
        //    long sku_id = pro_id * 100 + pr.pro_SKU;
        //    JObject sku_idObj = new JObject
        //    {
        //        {"sku_id",sku_id },
        //    };
        //    return sku_idObj;

        //}


    }
}